package guda.house.biz.impl;

import guda.grape.autogen.common.page.BizResult;
import guda.house.biz.SuiteBiz;
import guda.house.biz.constants.CommonResultCode;
import guda.house.biz.enums.SysVarTypeEnum;
import guda.house.biz.query.OfficeEmployeeQuery;
import guda.house.biz.query.SuiteQuery;
import guda.house.biz.query.SysVarQuery;
import guda.house.dal.dao.*;
import guda.house.dal.dataobject.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class SuiteBizImpl implements SuiteBiz {

    private final static Logger logger = LoggerFactory.getLogger(SuiteBizImpl.class);

    @Autowired
    private SuiteDAO suiteDAO;
    @Autowired
    private UnitDAO unitDAO;
    @Autowired
    private BuildingDAO buildingDAO;
    @Autowired
    private SysVarDAO sysVarDAO;
    @Autowired
    private VillageDAO villageDAO;
    @Autowired
    private OfficeEmployeeDAO officeEmployeeDAO;

    public BizResult detail(long id) {
        BizResult bizResult = new BizResult();
        try {
            SuiteDO suiteDO = suiteDAO.getSuiteById(id);
            bizResult.data.put("suiteDO", suiteDO);
            UnitDO unitById = unitDAO.getUnitById(suiteDO.getUnitId());
            if (unitById == null) {
                bizResult.code = CommonResultCode.RECORD_NOT_FOUND;
                return bizResult;
            }
            BuildingDO buildingById = buildingDAO.getBuildingById(unitById.getBuildingId());
            if (buildingById == null) {
                bizResult.code = CommonResultCode.RECORD_NOT_FOUND;
                return bizResult;
            }
            bizResult.data.put("unitDO", unitById);
            bizResult.data.put("buildingDO", buildingById);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("query Suite error", e);
        }
        return bizResult;
    }

    public BizResult list(SuiteQuery suiteQuery) {
        BizResult bizResult = new BizResult();
        try {
            int totalCount = suiteDAO.countForPage(suiteQuery);
            suiteQuery.setTotalCount(totalCount);
            List<SuiteDO> suiteList = suiteDAO.selectForPage(suiteQuery);
            bizResult.data.put("suiteList", suiteList);
            bizResult.data.put("query", suiteQuery);

            UnitDO unitById = unitDAO.getUnitById(suiteQuery.getUnitId());
            if (unitById == null) {
                bizResult.code = CommonResultCode.RECORD_NOT_FOUND;
                return bizResult;
            }
            BuildingDO buildingById = buildingDAO.getBuildingById(unitById.getBuildingId());
            if (buildingById == null) {
                bizResult.code = CommonResultCode.RECORD_NOT_FOUND;
                return bizResult;
            }
            bizResult.data.put("unitDO", unitById);
            bizResult.data.put("buildingDO", buildingById);

            bizResult.success = true;
        } catch (Exception e) {
            logger.error("view Suite list error", e);
        }
        return bizResult;
    }

    public BizResult delete(long id) {
        BizResult bizResult = new BizResult();
        try {
            suiteDAO.delSuiteById(id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("delete suite error", e);
        }
        return bizResult;
    }

    public BizResult create(SuiteDO suiteDO) {
        BizResult bizResult = new BizResult();
        try {
            //查询房号是否重复
            List<Long> bySuiteName = suiteDAO.checkRepeat(suiteDO.getSuiteName(), suiteDO.getUnitId());
            if(bySuiteName.size()>0){
                bizResult.code = CommonResultCode.SUITE_NAME_REPEAT;
                return bizResult;
            }
            long id = suiteDAO.insert(suiteDO);
            bizResult.data.put("id", id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("create Suite error", e);
        }
        return bizResult;
    }

    @Override
    public BizResult update(SuiteDO suiteDO) {
        BizResult bizResult = new BizResult();
        try {
            //查询房号是否重复
            List<Long> bySuiteName = suiteDAO.checkRepeat(suiteDO.getSuiteName(), suiteDO.getUnitId());
            if(bySuiteName.size()>1){
                bizResult.code = CommonResultCode.SUITE_NAME_REPEAT;
                return bizResult;
            }else if(bySuiteName.size() == 1 && bySuiteName.get(0).longValue() != suiteDO.getSuiteId().longValue()){
                bizResult.code = CommonResultCode.SUITE_NAME_REPEAT;
                return bizResult;
            }
            suiteDAO.updateByIdSelective(suiteDO);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("update Suite error", e);
        }
        return bizResult;
    }

    @Override
    public BizResult queryForInit(Long unitId) {
        BizResult bizResult = new BizResult();
        try {
            UnitDO unitById = unitDAO.getUnitById(unitId);
            if (unitById == null) {
                bizResult.code = CommonResultCode.RECORD_NOT_FOUND;
                return bizResult;
            }
            BuildingDO buildingById = buildingDAO.getBuildingById(unitById.getBuildingId());
            if (buildingById == null) {
                bizResult.code = CommonResultCode.RECORD_NOT_FOUND;
                return bizResult;
            }
//            //查询用水，用电类型
//            SysVarQuery sysVarQuery = new SysVarQuery();
//            sysVarQuery.setVillageId(buildingById.getVillageId());
//            sysVarQuery.setVarType(SysVarTypeEnum.elec.name());
//            sysVarQuery.setPageSize(500);
//            List<SysVarDO> elecSysVarList = sysVarDAO.selectForView(sysVarQuery);
//            bizResult.data.put("elecSysVarList", elecSysVarList);
//
//            sysVarQuery.setVarType(SysVarTypeEnum.water.name());
//            List<SysVarDO> waterSysVarList = sysVarDAO.selectForView(sysVarQuery);
//            bizResult.data.put("waterSysVarList", waterSysVarList);


            SysVarQuery sysVarQuery = new SysVarQuery();
            sysVarQuery.setVillageId(buildingById.getVillageId());
            sysVarQuery.setVarType(SysVarTypeEnum.building.name());
            sysVarQuery.setPageSize(500);
            List<SysVarDO> sysVarBuildingTypeList = sysVarDAO.selectForPage(sysVarQuery);
            bizResult.data.put("sysVarBuildingTypeList",sysVarBuildingTypeList);

            sysVarQuery = new SysVarQuery();
            sysVarQuery.setVillageId(buildingById.getVillageId());
            sysVarQuery.setPageSize(500);
            sysVarQuery.setVarType(SysVarTypeEnum.office.name());
            List<SysVarDO> officeList = sysVarDAO.selectForView(sysVarQuery);
            bizResult.data.put("officeList", officeList);


            bizResult.data.put("unitDO", unitById);
            bizResult.data.put("buildingDO", buildingById);
            bizResult.data.put("villageDO", villageDAO.getVillageById(buildingById.getVillageId()));
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("update Suite error", e);
        }
        return bizResult;
    }


    public BizResult queryForInitBySuiteId(Long suiteId) {
        BizResult bizResult = new BizResult();
        try {
            SuiteDO suiteDO = suiteDAO.getSuiteById(suiteId);
            UnitDO unitById = unitDAO.getUnitById(suiteDO.getUnitId());
            if (unitById == null) {
                bizResult.code = CommonResultCode.RECORD_NOT_FOUND;
                return bizResult;
            }
            BuildingDO buildingById = buildingDAO.getBuildingById(unitById.getBuildingId());
            if (buildingById == null) {
                bizResult.code = CommonResultCode.RECORD_NOT_FOUND;
                return bizResult;
            }
            //查询用水，用电类型
//            SysVarQuery sysVarQuery = new SysVarQuery();
//            sysVarQuery.setVillageId(buildingById.getVillageId());
//            sysVarQuery.setVarType(SysVarTypeEnum.elec.name());
//            sysVarQuery.setPageSize(500);
//            List<SysVarDO> elecSysVarList = sysVarDAO.selectForView(sysVarQuery);
//            bizResult.data.put("elecSysVarList", elecSysVarList);
//
//            sysVarQuery.setVarType(SysVarTypeEnum.water.name());
//            List<SysVarDO> waterSysVarList = sysVarDAO.selectForView(sysVarQuery);
//            bizResult.data.put("waterSysVarList", waterSysVarList);

            SysVarQuery sysVarQuery = new SysVarQuery();
            sysVarQuery.setVillageId(buildingById.getVillageId());
            sysVarQuery.setPageSize(500);
            sysVarQuery.setVarType(SysVarTypeEnum.office.name());
            List<SysVarDO> officeList = sysVarDAO.selectForView(sysVarQuery);
            bizResult.data.put("officeList", officeList);

            SuiteDO suiteById = suiteDAO.getSuiteById(suiteId);
            if(suiteById.getOfficeVarId()!=null) {
                OfficeEmployeeQuery officeEmployeeQuery = new OfficeEmployeeQuery();
                officeEmployeeQuery.setVarId(suiteById.getOfficeVarId());
                List<OfficeEmployeeDO> officeEmployeeDOList = officeEmployeeDAO.selectForPage(officeEmployeeQuery);
                bizResult.data.put("employeeList",officeEmployeeDOList);
            }

            bizResult.data.put("unitDO", unitById);
            bizResult.data.put("buildingDO", buildingById);
            bizResult.data.put("villageDO", villageDAO.getVillageById(buildingById.getVillageId()));
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("update Suite error", e);
        }
        return bizResult;
    }

    @Override
    public BizResult listForCommon(SuiteQuery suiteQuery) {
        BizResult bizResult = new BizResult();
        try {
            int totalCount = suiteDAO.countForPage(suiteQuery);
            suiteQuery.setTotalCount(totalCount);
            List<SuiteDO> suiteList = suiteDAO.selectForPage(suiteQuery);
            bizResult.data.put("suiteList", suiteList);
            bizResult.data.put("query", suiteQuery);

            bizResult.success = true;
        } catch (Exception e) {
            logger.error("view Suite list error", e);
        }
        return bizResult;
    }


}
